﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using DeepTalkRenderServiceLib;
using System.Threading;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Reflection;
using System.Configuration;

namespace DeepTalkRenderShell.App
{
    class Program
    {
        /// <summary>
        /// Runs the rendering from the command line.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            ///
            /// Parse the arguments coming in...
            /// 

            Stack<string> argStack = new Stack<string>();
            string serviceLocal = "TestingBinding";

            for (int i = args.Length-1; i >= 0; i--)
            {
                argStack.Push(args[i]);
            }

            string argBeingProcessed = null;
            while (argStack.Count > 0)
            {
                argBeingProcessed = argStack.Pop();
                switch (argBeingProcessed)
                {
                    case "BindingTest":
                        serviceLocal = "TestingBinding";
                        break;

                    case "BindingLocalIIS":
                        serviceLocal = "LocalIISBinding";
                        break;

                    case "BindingProduction":
                        serviceLocal = "ProductionIISBinding";
                        break;

                    default:
                        Console.WriteLine("Command line must be BindingTest or BindingLocalIIS!");
                        break;
                }
            }
            
            ///
            /// Now run the program.
            /// 

            var program = new Program();
            program.Run(serviceLocal);
        }

        /// <summary>
        /// Get MEF up and running and other basic initing
        /// </summary>
        private Program()
        {
            Compose();
        }

        /// <summary>
        /// Do the running...
        /// </summary>
        private void Run(string dtRenderBindingName)
        {
            ///
            /// See if we can find the correct item from our settings to be doing the rendering
            /// 

            string renderObjectTypeName = (string) Settings.Default["RenderType"];
            if (renderObjectTypeName == null)
            {
                Console.WriteLine("RenderType was not set!");
                return;
            }

            var renderObjSpec = (from r in AllTalkRenderingPlubins
                            where (string)r.Metadata["Name"] == renderObjectTypeName
                            select r).FirstOrDefault();
            if (renderObjSpec == null)
            {
                Console.WriteLine("Don't know of any named rendering object '" + renderObjectTypeName + "'.");
                return;
            }

            var renderObj = renderObjSpec.Value;

            ///
            /// Start the rendering service.
            /// 

            RenderMeetings.Start(renderObj.RenderTalk, dtRenderBindingName);

            ///
            /// Loop until someone kills us
            /// 

            while (true)
            {
                Thread.Sleep(60 * 60 * 1000);
            }
        }

        /// <summary>
        /// List of everything we have that can actually do the rendering.
        /// </summary>
        [ImportMany(typeof(IRenderTalk))]
        private IEnumerable<Lazy<IRenderTalk, IDictionary<string,object>>> AllTalkRenderingPlubins { get; set; }

        /// <summary>
        /// Init MEF
        /// </summary>
        private void Compose()
        {
            var catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
            var container = new CompositionContainer(catalog);
            container.ComposeParts(this);
        }
    }
}
